﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Wilke.SharePoint.Tools
{
    internal static class ConfigurationHelper
    {
        internal static string SharePointVersion
        {
            get
            {
                string sharePointVersion = String.Empty;

                if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.ConfigKeySharePointVersion))
                {
                    sharePointVersion = ConfigurationManager.AppSettings[Constants.ConfigKeySharePointVersion];
                }

                if(String.IsNullOrEmpty(sharePointVersion))
                {
                     sharePointVersion = Constants.DefaultSharePointVersion;
                }

                return sharePointVersion;
            }
        }

        internal static List<string> FrequentlyUsedNodeNames
        {
            get
            {
                List<String> frequentlyUsedNodeNames = new List<string>();

                if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.ConfigKeyFrequentlyUsedNodeNames))
                {
                    string configurationValue = ConfigurationManager.AppSettings[Constants.ConfigKeyFrequentlyUsedNodeNames];

                    string[] nodes = configurationValue.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                    frequentlyUsedNodeNames.AddRange(nodes);
                }

                if (frequentlyUsedNodeNames.Count == 0)
                {
                    frequentlyUsedNodeNames.AddRange(Constants.DefaultFrequentlyUsedNodeNames.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries));
                }

                return frequentlyUsedNodeNames;
            }
        }

        internal static List<string> AutoLookupNodesToIgnore 
        {
            get 
            {
                List<String> autoLookupNodesToIgnore = new List<string>();

                if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.ConfigKeyAutoLookupNodesToIgnore))
                {
                    string configurationValue = ConfigurationManager.AppSettings[Constants.ConfigKeyAutoLookupNodesToIgnore];

                    string[] nodesToIgnore = configurationValue.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                    autoLookupNodesToIgnore.AddRange(nodesToIgnore);
                }

                if (autoLookupNodesToIgnore.Count == 0)
                {
                    autoLookupNodesToIgnore.AddRange(Constants.DefaultAutoLookupNodesToIgnore.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries));
                }

                return autoLookupNodesToIgnore;
            } 
        }

        internal static List<string> DifferentiatingAttributeNames
        {
            get
            {
                List<String> differentiatingAttributeNames = new List<string>();

                if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.ConfigKeyAutoLookupDifferentiatingAttributeNames))
                {
                    string configurationValue = ConfigurationManager.AppSettings[Constants.ConfigKeyAutoLookupDifferentiatingAttributeNames];

                    string[] attributeNames = configurationValue.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                    differentiatingAttributeNames.AddRange(attributeNames);
                }

                if (differentiatingAttributeNames.Count == 0)
                {
                    differentiatingAttributeNames.AddRange(Constants.DefaultDifferentiatingAttributeNames.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries));
                }

                return differentiatingAttributeNames;
            }
        }

        internal static string InetPubPath
        {
            get
            {
                string inetPubPath = String.Empty;

                if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.ConfigKeyInetPubPath))
                {
                    inetPubPath = ConfigurationManager.AppSettings[Constants.ConfigKeyInetPubPath];
                }

                if (String.IsNullOrEmpty(inetPubPath))
                {
                    inetPubPath = Constants.DefaultInetPubPath;
                }

                return inetPubPath;
            }
        }

        internal static string CodeGenerationNamespace
        {
            get
            {
                string codeGenerationNamespace = String.Empty;

                if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.ConfigKeyCodeGenerationNamespace))
                {
                    codeGenerationNamespace = ConfigurationManager.AppSettings[Constants.ConfigKeyCodeGenerationNamespace];
                }

                if (String.IsNullOrEmpty(codeGenerationNamespace))
                {
                    codeGenerationNamespace = Constants.DefaultCodeGenerationNamespace;
                }

                return codeGenerationNamespace;
            }
        }

        internal static string DefaultOwner
        {
            get
            {
                string defaultOwner = String.Empty;

                if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.ConfigKeyDefaultOwner))
                {
                    defaultOwner = ConfigurationManager.AppSettings[Constants.ConfigKeyDefaultOwner];
                }

                if (String.IsNullOrEmpty(defaultOwner))
                {
                    defaultOwner = Constants.DefaultOwner;
                }

                return defaultOwner;
            }
        }
    }
}
